package com.bff.slimserver.musicjukebox.ui.components;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalSliderUI;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author bfindeisen
 */
public class TrackPositionSlider extends JSlider {

    public TrackPositionSlider() {
        this(JSlider.HORIZONTAL);
    }

    public TrackPositionSlider(int orientation) {
        super(orientation);
        //setUI(new MySliderUI());
        
        Icon icon = new ImageIcon(getClass().getResource("/resources/slimserver/blue_dot.png"));
        UIDefaults defaults = UIManager.getDefaults();
        defaults.put("Slider.horizontalThumbIcon", icon);
        setPaintLabels(false);
        //putClientProperty("JSlider.isFilled", Boolean.TRUE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("Gradient Slider Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new FlowLayout());
                frame.add(new TrackPositionSlider(JSlider.VERTICAL));
                frame.add(new TrackPositionSlider(JSlider.HORIZONTAL));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MySliderUI extends MetalSliderUI {

    public void paintTrack(Graphics g) {

        super.paintTrack(g);
        Color trackColor = !slider.isEnabled() ? MetalLookAndFeel.getControlShadow() : slider.getForeground();

        boolean leftToRight = true;//MetalUtils.isLeftToRight(slider);

        g.translate(trackRect.x, trackRect.y);

        int trackLeft = 0;
        int trackTop = 0;
        int trackRight = 0;
        int trackBottom = 0;

        // Draw the track
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
            trackBottom = (trackRect.height - 1) - getThumbOverhang();
            trackTop = trackBottom - (getTrackWidth() - 1);
            trackRight = trackRect.width - 1;
        } else {
            if (leftToRight) {
                trackLeft = (trackRect.width - getThumbOverhang()) -
                        getTrackWidth();
                trackRight = (trackRect.width - getThumbOverhang()) - 1;
            } else {
                trackLeft = getThumbOverhang();
                trackRight = getThumbOverhang() + getTrackWidth() - 1;
            }
            trackBottom = trackRect.height - 1;
        }

        if (slider.isEnabled()) {
            g.setColor(MetalLookAndFeel.getControlDarkShadow());
            g.drawRect(trackLeft, trackTop,
                    (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1);

            g.setColor(MetalLookAndFeel.getControlHighlight());
            g.drawLine(trackLeft + 1, trackBottom, trackRight, trackBottom);
            g.drawLine(trackRight, trackTop + 1, trackRight, trackBottom);

            g.setColor(MetalLookAndFeel.getControlShadow());
            g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1);
            g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2);
        } else {
            g.setColor(MetalLookAndFeel.getControlShadow());
            g.drawRect(trackLeft, trackTop,
                    (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1);
        }

        // Draw the fill
        //if ( filledSlider ) {
        int middleOfThumb = 0;
        int fillTop = 0;
        int fillLeft = 0;
        int fillBottom = 0;
        int fillRight = 0;

        if (slider.getOrientation() == JSlider.HORIZONTAL) {
            middleOfThumb = thumbRect.x + (thumbRect.width / 2);
            middleOfThumb -= trackRect.x; // To compensate for the g.translate()
            fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
            fillBottom = !slider.isEnabled() ? trackBottom - 1 : trackBottom - 2;

            if (!drawInverted()) {
                fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
                fillRight = middleOfThumb;
            } else {
                fillLeft = middleOfThumb;
                fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;
            }
        } else {
            middleOfThumb = thumbRect.y + (thumbRect.height / 2);
            middleOfThumb -= trackRect.y; // To compensate for the g.translate()
            fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
            fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;

            if (!drawInverted()) {
                fillTop = middleOfThumb;
                fillBottom = !slider.isEnabled() ? trackBottom - 1 : trackBottom - 2;
            } else {
                fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
                fillBottom = middleOfThumb;
            }
        }

        if (slider.isEnabled()) {
            g.setColor(slider.getBackground());
            g.drawLine(fillLeft, fillTop, fillRight, fillTop);
            g.drawLine(fillLeft, fillTop, fillLeft, fillBottom);

            g.setColor(MetalLookAndFeel.getControlShadow());
            g.fillRect(fillLeft + 1, fillTop + 1,
                    fillRight - fillLeft, fillBottom - fillTop);

            Graphics2D g2 = (Graphics2D) g;
            GradientPaint gPaint = null;
            if (slider.getOrientation() == JSlider.HORIZONTAL) {
                gPaint = new GradientPaint(trackLeft, 0, Color.BLUE,
                        trackRight, 0, Color.RED);
            } else {
                gPaint = new GradientPaint(0, trackBottom, Color.BLUE,
                        0, trackTop, Color.RED);
            }
            g2.setPaint(gPaint);
            g2.fillRect(fillLeft + 1, fillTop + 1,
                    fillRight - fillLeft, fillBottom - fillTop);

        } else {
            g.setColor(MetalLookAndFeel.getControlShadow());
            g.fillRect(fillLeft, fillTop,
                    fillRight - fillLeft, trackBottom - trackTop);
        }
        //}

        g.translate(-trackRect.x, -trackRect.y);
    }
}

